//
//  EditBoxController.m
//  LeaveLight
//
//  Created by 张 on 2019/3/11.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "EditBoxController.h"

@implementation EditBoxController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = UIColorMakeWithHex(@"#F5F5F5");
    [self setTitle:@"我的草稿箱"];
    [self setRightButtonWithTitle:@"编辑" image:[UIImage imageNamed:@""] himage:[UIImage imageNamed:@""] action:@selector(editClick:)];
    self.viewTop.constant = NavigationBarHeight;
    self.isEdit = NO;
    self.chooseArr = [NSMutableArray array];
    [self.collectionView registerNib:[UINib nibWithNibName:@"EditBoxItem" bundle:nil] forCellWithReuseIdentifier:@"EditBoxItem"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (CONTENTMANAGER.templateArrs.count == 0) {
        TempView * temp = [[TempView alloc]initWithFrame:CGRectMake(0, 0, self.collectionView.qmui_width, SCREEN_HEIGHT) img:@"草稿箱的空白页" placeStr:@"你的箱子空空的"];
        [self.collectionView addSubview:temp];
    }else
    {
        [self.collectionView reloadData];
    }
}

- (void)editClick:(UIButton*)btn
{
    if (self.isEdit) {
        [btn setTitle:@"编辑" forState:0];
        self.isEdit = NO;
        if (self.chooseArr.count > 0) {
            for (TemplateModel * mod in self.chooseArr) {
                [CONTENTMANAGER deleteTemplateWith:mod];
            }
        }
    }else{
        [btn setTitle:@"删除" forState:0];
        self.isEdit = YES;
    }
    [self.chooseArr removeAllObjects];
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
// 指定Section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 指定section中的collectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return CONTENTMANAGER.templateArrs.count;
}

// 配置section中的collectionViewCell的显示
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TemplateModel * model = CONTENTMANAGER.templateArrs[indexPath.row];
    EditBoxItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EditBoxItem" forIndexPath:indexPath];
    if(self.isEdit){
        [cell.chooseImg setHidden:NO];
        if ([self.chooseArr containsObject:model]) {
            [cell.chooseImg setImage:[UIImage imageNamed:@"选择_sel"]];
        }else{
            [cell.chooseImg setImage:[UIImage imageNamed:@"选择"]];
        }
    }else{
        [cell.chooseImg setHidden:YES];
    }
    [cell.baseImg setImage:model.completeImg];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TemplateModel * model = CONTENTMANAGER.templateArrs[indexPath.row];
    if (self.isEdit) {
        if ([self.chooseArr containsObject:model]) {
            [self.chooseArr removeObject:model];
        }else{
            [self.chooseArr addObject:model];
        }
        [collectionView reloadData];
    }else{
        EditController * edit = [[EditController alloc]initWithNibName:@"EditController" bundle:nil];edit.isEdit = YES;
        model.chicken_model.is_edit = YES;
        edit.model = model;
        edit.isEdit = YES;
        [self.navigationController pushViewController:edit animated:YES];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((SCREEN_WIDTH - 40 - 13) / 2,(SCREEN_WIDTH - 40 - 13) / 2 * 260/161);
}

// 设置整个组的缩进量是多少
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(20, 20, 20, 20);
}

// 设置最小行间距，也就是前一行与后一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 20;
}

// 设置最小列间距，也就是左行与右一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 13;
}

@end
